<template>
  <div class="container">
    <div class="cart_title">
      <div class="cart_lf">
        <div class="cart_logo"><img src="/images/logo.png" alt="" /></div>
        <div class="cart_text">
          <a href="">请确认订单</a>
        </div>
      </div>
      <div class="cart_rg">
        <div class="cart_username" v-show="name">{{ name }}</div>
      </div>
    </div>
    <div class="cart_content">
      <!-- 获取地址 -->
      <div class="order-box">
        <div class="addr">
          <div class="addr-title">收货地址</div>
          <!-- <div class="addr-box">
            <div class="add">+</div>
            <div class="addr-desc">添加新地址</div>
          </div> -->
          <div class="address-list">
            <a-modal v-model="visible" title="Basic Modal" @ok="handleOk">
            </a-modal>
            <div class="address_list">
              <div
                class="address"
                v-for="(item, index) in addrlist"
                :key="index"
              >
                <div class="address_name">{{ item.name }}</div>
                <div class="address_phone">{{ item.phone }}</div>
                <div class="address_area">{{ item.area_name }}</div>
                <div class="address_desc"></div>
                <div class="action">
                  <a-button
                    class="btn1"
                    type="primary"
                    @click="showEditModal(item.id)"
                  >
                    修改
                  </a-button>
                  <a-button
                    class="btn2"
                    type="primary"
                    @click="showDelModal(item.id)"
                  >
                    删除
                  </a-button>
                </div>
              </div>
              <div class="address" @click="showAddModal">
                <div class="addIcon">+</div>
                <div class="addr-title">获取地址</div>
              </div>
            </div>
            <a-modal v-model="visibleEdit" :title="titleName" @ok="handleOk">
              <a-input placeholder="姓名" v-model="checkedItem.name"></a-input>
              <a-input placeholder="手机" v-model="checkedItem.phone"></a-input>
              <a-cascader
                :options="option"
                placeholder="请选择城市"
                @change="onChange"
                v-model="checkedItem.area"
              />
              <a-input
                placeholder="详细地址"
                v-model="checkedItem.desc"
              ></a-input>
            </a-modal>

            <a-modal v-model="visibleDel" title="删除" @ok="handleOk">
              <p>你确定删除吗</p>
            </a-modal>
          </div>
        </div>
        <div class="order-desc">
          <!-- 商品 -->
          <div class="order-title">商品及优惠卷</div>
          <div
            class="order-products"
            v-for="(item, index) in selectProduct"
            :key="index"
          >
            <div class="product-img">
              <img :src="item.s_good.s_goods_photos[0].path" alt="" />
            </div>
            <div class="product-name">
              <span>{{ item.s_good.name }}</span>
            </div>
            <div class="product-num">
              <span>{{ item.s_good.price }}元x{{ item.num }}</span>
            </div>
            <div class="product-price">
              <span>{{ item.s_good.price }}元</span>
            </div>
          </div>
          <!-- 配送方式 -->
          <div class="send">
            <h2>配送方式</h2>
            <span>包邮</span>
          </div>
          <!-- 发票 -->
          <div class="ticke">
            <h2>发票</h2>
            <span>电子发票</span> <span>个人</span> <span>商品明细</span>
          </div>
          <!-- 右侧详情 -->
          <div class="detail">
            <div class="item">
              <span class="item-name">商品件数：</span>
              <span class="item-val">{{ totalCount }}件</span>
            </div>
            <div class="item">
              <span class="item-name">商品总价：</span>
              <span class="item-val">{{ totalPrice }}元</span>
            </div>
            <div class="item">
              <span class="item-name">优惠活动：</span>
              <span class="item-val">0元</span>
            </div>
            <div class="item">
              <span class="item-name">运费：</span>
              <span class="item-val">0元</span>
            </div>
            <div class="item-total">
              <span class="item-name">应付总额：</span>
              <span class="item-val">{{ totalPrice }}元</span>
            </div>
          </div>
          <div class="btn-box">
            <button class="back" @click="$router.push('/cart')">
              返回购物车
            </button>
            <button class="order" @click="ToOrder">立即下单</button>
          </div>
        </div>
      </div>
    </div>
    <Footer />
  </div>
</template>

<script>
import Footer from "../components/Footer.vue";
import {
  getCity,
  getCartInfo,
  getUserInfo,
  getAddress,
  editAddress,
  delAddress,
  addAddress,
} from "@/assets/js/request.js";
import { mapActions, mapState, mapMutations } from "vuex";
import { AddOrder, delCart } from "../assets/js/request";
export default {
  data() {
    return {
      cartList: [],
      CartOrderList: [],
      cityName: "",
      userAction: "",
      checkedItem: {},
      titleName: "",
      option: [],
      visible: false,
      visibleAdd: false,
      visibleEdit: false,
      visibleDel: false,
      addrlist: [],
      selectProduct: [],
      currentIndex: 0,
    };
  },
  async created() {
    //获取城市
    let option = await getCity();
    // console.log(option);
    this.option = option;
    // 获取添加地址的列表
    let res1 = await getAddress();
    // console.log(res1);
    this.addrlist = res1;

    //获取用户信息
    let res = await getCartInfo();
    // console.log(res);
    this.cartList = res;
    getUserInfo().then((item) => {
      this.userId = item.id;
    });

    // 获取购物车商品
    this.getCar();
  },
  computed: {
    ...mapState(["name"]),
    //总数量
    totalCount() {
      let count = 0;
      this.selectProduct.forEach((ele) => {
        count += ele.num;
      });
      return count;
    },
    //总价格
    totalPrice() {
      let Pricesum = 0;
      this.selectProduct.forEach((ele) => {
        Pricesum += ele.num * ele.s_good.price;
      });
      return Pricesum;
    },
  },
  methods: {
    ...mapActions(["SaveUsername"]),
    onChange(value, selectedOptions) {
      console.log(value); //返回一个数组
      console.log(selectedOptions);
      console.log(selectedOptions); //返回一个数组 但是显示的更加详细
      // let paths =this.$refs.abc.()[0].
      this.cityname = selectedOptions.map((ele) => {
        return ele.cityname;
      });
      console.log(this.cityname);
    },
    showEditModal(id) {
      this.userAction = 1;
      //this.userAction=1; 记录用户行为 目的：将来handleOk
      this.visibleEdit = true;
      this.titleName = "修改";
      this.checkedItem.id = id;
    },
    showDelModal(id) {
      this.userAction = 2; //删除
      this.visibleDel = true;
      console.log(id);
      this.checkedItem.id = id;
      this.titleName = "删除收货地址";
    },
    showAddModal() {
      this.userAction = 0;
      this.visibleEdit = true;
      this.titleName = "添加";
    },

    async handleOk() {
      if (this.userAction === 0) {
        //发送添加请求
        let { checkedItem } = this;
        let params = {
          name: checkedItem.name,
          phone: checkedItem.phone,
          area: `${checkedItem.area[0]},${checkedItem.area[1]},${checkedItem.area[2]}`,
          area_name: `${this.cityname[0]}${this.cityname[1]}${this.cityname[2]}`,
          desc: checkedItem.desc,
        };
        addAddress(params);
      }
      if (this.userAction === 1) {
        //   修改请求
        let { checkedItem } = this;
        let params = {
          name: checkedItem.name,
          phone: checkedItem.phone,
          area: `${checkedItem.area[0]},${checkedItem.area[1]},${checkedItem.area[2]}`,
          area_name: `${this.cityname[0]}${this.cityname[1]}${this.cityname[2]}`,
          desc: checkedItem.desc,
        };
        let id = this.checkedItem.id;
        editAddress(id, params)
          .then((res) => {
            this.area_name = res.area_name[0];
          })
          .then(() => {
            getAddress().then((res) => {
              this.list = res;
            });
            this.visibleEdit = false;
          });
      }
      if (this.userAction === 2) {
        //   删除请求
        let id = this.checkedItem.id;
        console.log(this.checkedItem.id);
        delAddress(id).then(() => {
          getAddress().then((res) => {
            this.list = res;
          });
          this.visibleDel = false;
        });
      }
    },
    ToOrder() {
      let item = this.addrlist[this.currentIndex];
      console.log(item);
      let data = {
        addr_id: item.id,
        project_id: 7,
        goods_info: [],
      };
      this.selectProduct.forEach((ele) => {
        data.goods_info.push({
          id: ele.goods_id,
          num: ele.num,
        });
      });
      AddOrder(data).then((res) => {
        // console.log(res);
        sessionStorage.setItem(
          "selectCart",
          JSON.stringify(this.selectProduct)
        );
        this.$router.push({
          path: "/pay",
          query: {
            id: res.id,
            selectProduct: this.selectProduct,
            totalPrice: this.totalPrice,
          },
        });
      });
      this.delOrderCart();
    },
    delOrderCart() {
      this.selectProduct.forEach((ele) => {
        delCart(ele.id).then((res) => {
          console.log(res);
        });
      });
    },
    getCar() {
      let CartOrderList = JSON.parse(sessionStorage.getItem("checkout")) || [];
      this.selectProduct = CartOrderList.filter((ele) => ele.checked == true);
      console.log(this.selectProduct);
    },
  },
  components: {
    Footer,
  },
};
</script>
<style lang="scss" scoped>
@import "../assets/scss/config.scss";
@import "../assets/scss/reset.scss";
.cart_title {
  width: 100%;
  height: 100px;
  background-color: #fff;
  margin: 0 auto;
  position: relative;
  .cart_lf {
    position: absolute;
    left: 15%;
    // display: inline-block;
    float: left;
    display: block;
    .cart_logo {
      margin: 20px 0;
      float: left;
      img {
        width: 50px;
        height: 50px;
      }
    }
    .cart_text {
      height: 100px;
      float: left;
      margin: 0 auto;
      a {
        display: block;
        margin: 0 auto;
        font-size: 22px;
        color: black;
        margin: 30px 10px;
      }
    }
    .cart_tips {
      display: inline-block;
      width: 300px;
      height: 30px;
      margin: 0 auto;
      float: left;
      margin: 35px 0px;
      font-size: 14px;
      color: #666;
      a {
        color: #666;
      }
    }
  }
  .cart_rg {
    float: right;
    position: absolute;
    margin: 35px 0;
    right: 15%;
  }
}
.cart_content {
  border-top: 2px solid orangered;
  background-color: #f5f5f5;
  width: 100%;
  padding-bottom: 20px;
  // margin: 0 auto;
  .order-box {
    width: 80%;
    // height: 600px;
    background-color: white;
    margin: 20px auto;
    margin-bottom: 20px;
    .addr-title {
      text-align: left;
      padding: 20px 25px 5px;
      font-size: 18px;
    }
    .addr-box {
      width: 250px;
      height: 150px;
      border: 1px solid #ccc;
      margin: 5px 20px;
    }
    .add {
      width: 30px;
      height: 30px;
      padding: 5px;
      margin: 30px auto;
      background-color: #ccc;
      border-radius: 50%;
      color: white;
    }
    .address-list {
      div {
        margin-bottom: 10px;
      }

      .address_list {
        display: flex;
        justify-content: flex-start;
        //   align-items: center;
        flex-wrap: wrap;
        margin-top: 10px;
        .address {
          padding: 10px 20px;
          margin: 5px 20px;
          width: 300px;
          height: 150px;
          text-align: left;
          // background-color: skyblue;
          border: 1px solid #e0e0e0;
          position: relative;
          .addIcon {
            width: 30px;
            height: 30px;
            margin: 20px auto 5px;
            background-color: #666;
            border-radius: 50%;
            padding-top: 5px;
            text-align: center;
            color: white;
          }
          .addr-title {
            text-align: center;
            font-size: 16px;
          }
          .address_name {
            font-size: 20px;
          }
          .address_phone {
            font-size: 16px;
            color: #757575;
          }
          .address_area {
            font-size: 16px;
            color: #757575;
          }
          .address_desc {
            font-size: 16px;
            color: #757575;
          }
          .action {
            display: flex;
            margin: 0 60px;

            .btn1,
            .btn2 {
              margin: 5px 10px;
              width: 50px;
              height: 20px;
              border: none;
              background-color: #ccc;
              color: white;
            }
          }
        }
      }
    }
  }
}
.line {
  height: 1px;
  width: 100%;
  margin: 20px auto;
  border: 1px solid #ccc;
}
.order-desc {
  padding: 60px 44px 33px 0;

  // border-bottom: 1px solid #f5f5f5;
  text-align: right;
  font-size: 16px;
  color: #666;
  .order-title {
    text-align: left;
    margin: 30px 30px;
    font-size: 18px;
  }
  .order-products {
    width: 90%;

    margin: 20px auto;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-bottom: 20px;
    border-bottom: 1px solid #ccc;
    .product-img {
      width: 120px;
      img {
        width: 40px;
        height: 35px;
      }
    }
    .product-name {
      text-align: center;
      width: 200px;
    }
    .product-num {
      width: 100px;
    }
    .product-price {
      width: 100px;
      color: #ff6600;
    }
  }
  .send,
  .ticke {
    display: flex;
    h2 {
      padding: 10px 20px;
      float: left;
      font-size: 16px;
      color: black;
    }
    span {
      padding: 10px 30px;
      float: left;
      font-size: 16px;
      color: #ff6600;
    }
  }
  .detail {
    padding: 50px 44px 33px 0;
    border-bottom: 1px solid #f5f5f5;
    text-align: right;
    font-size: 16px;
    color: #666;
    .item {
      margin-bottom: 10px;
      .item-val {
        color: $colorA;
        display: inline-block;
        width: 100px;
      }
    }
    .item-total {
      .item-val {
        font-size: 28px;
        color: $colorA;
      }
    }
  }
  .btn-box {
    width: 300px;
    float: right;
    display: flex;

    .back {
      display: block;
      width: 200px;
      height: 30px;
      margin: 1px;
      background-color: #ccc;
      border: none;
      color: white;
    }
    .order {
      display: block;
      width: 200px;
      height: 30px;
      margin: 1px;
      background-color: #ff6600;
      border: none;
      color: white;
    }
  }
}
</style>